<template>
  <div id="login-box">
    <div id="left-image">
      <!--   左侧图片   -->
    </div>
    <div id="right-form">
      <p id="login-title">账号登录</p>
      <!--   登录表单   -->
      <div class="input" v-if="currentPage==='logined'">
        <el-input
            class="username"
            v-model="user_input"
            placeholder="请输入用户名/邮箱/手机号"
            type="text"
            clearable
        />
        <el-input
            class="password"
            v-model="pwd_input"
            placeholder="请输入密码"
            type="password"
            show-password
        />
        <el-button type="primary" class="login-btn" @click="login">登 录</el-button>
      </div>

      <!--  忘记密码表单   -->
      <div class="input" v-if="currentPage==='forgeted'" id="forget-form">
        <el-input v-model="forgetAccount" class="forget-username"
                  placeholder="请输入邮箱或手机号"/>
        <el-button type="success" class="send-captcha">发送验证码</el-button>
        <el-input v-model="forgetCaptcha" class="forget-captcha"
                  placeholder="请输入验证码"/>
      </div>


      <!--       帮助菜单选项-->
      <div id="help">
        <el-button type="text" @click="forget" v-if="currentPage==='logined'">忘记密码</el-button>
        <el-button type="text" @click="logined" v-else-if="currentPage==='forgeted'">返回登录</el-button>
        <el-button type="text" @click="">注册账号</el-button>
      </div>
      <!--  协议  -->
      <div id="protocol">
        <el-checkbox v-model="checked">我已阅读并同意</el-checkbox>
        <el-button type="text" @click="readProtocol">《用户服务协议》</el-button>
      </div>
      <el-dialog v-model="dialogVisible" center width="500px">
        <div slot="title">
          用户服务协议
        </div>
        <span>
          暂时没想好
        </span>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">不同意</el-button>
            <el-button type="primary" @click="agree">
              同意该协议
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>


</template>

<script>

import * as LoginAPI from "@/api/login.js";
import router from "@/router/router.js";

export default {

  data() {
    return {
      pwd_input: '', // 密码
      user_input: '', // 用户名
      checked: false,// 是否同意协议
      dialogVisible: false, //展示对话框
      currentPage: 'logined',//当前页面
      forgetAccount: '', //忘记密码账号
      forgetCaptcha: '', //验证码
    }
  },
  methods: {
    logined() {
      this.currentPage = 'logined'
    },
    forget() {
      this.currentPage = 'forgeted'
    },
    agree() {
      this.dialogVisible = false
      this.checked = true
      this.$message.success('已同意协议')
    },
    readProtocol() {
      this.dialogVisible = true
    }
    ,
    login() {
      // 是否勾选协议
      if (!this.checked) {
        this.$message.error('请勾选协议')
        return
      }
      // 验证 表单输入是否合法
      if (this.user_input.trim() === '' || this.pwd_input.trim() === '') {
        this.$message.error('用户名或密码不能为空')
        return;
      }

      //登录请求
      let data = {
        username: this.user_input,
        password: this.pwd_input
      }
      try {
        LoginAPI.loginReq(data).then(res => {
          if (res.code === 500)
            this.$message.error(res.data)
          if (res.code === 200) {
            this.$message.success(res.data)
            router.push('/home')

          }
        })
      } catch (err) {
        console.log(err)
      }
    }
  }

}

</script>


<style scoped>

* {
  margin: 0;
  padding: 0;
}

#login-box {
  background-color: green;
  width: 800px;
  height: 400px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#left-image {
  background-color: darkcyan;
  width: 50%;
  height: 100%;
  float: left;
  background-image: url("../../assets/icon/index.png");
  background-size: 100% 100%;

}

#right-form {
  background-color: #ccebd0;
  width: 50%;
  height: 100%;
  float: right;
}

#login-title {
  color: #35b1db;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  margin-top: 20%;
}

.input {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.password, .username {
  width: 80%;
  margin: 10px auto;

}

.username {
  margin-top: 50px;
}

.login-btn {
  margin-top: 20px;
  width: 60%;
}

#help {
  margin: 15px 34%;
}

#protocol {
  margin: 0 20%;
}


.forget-username {
  margin-left: 50px;
  float: left;
}

.send-captcha {
  margin-left: 50px;
  float: right;
}

#forget-form {
  display: grid;
  grid-template-columns: 60% 40%;
  --el-notification-group-margin-right: 5px;
  padding: 40px;
}

.forget-captcha {
  margin-left: 50px;
  margin-top: 20px;
  width: 270px;
}
</style>